﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MvcEngine.Core.Utils
{
    /// <summary>
    /// Static class to validate the string representation of Guid. This is a workaround/alternative for missing TryParse method in 
    /// <see cref="T:System.Guid"/>.
    /// </summary>
    public static class GuidValidator
    {
        private static string pattern = "^[A-Fa-f0-9]{32}$|" +
                "^[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}$|" +
                "^({)[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(})$|" +
                "^(\\()[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(\\))$|" +
                "^[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(})$|" +
                "^({)[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$";

        private static Regex regex = new Regex(pattern, RegexOptions.Compiled);

        /// <summary>
        /// Validates a string containing Guid.
        /// </summary>
        /// <param name="s">A <see cref="System.String"/> containing Guid value to validate.</param>
        /// <returns>True if a string passed is a valid Guid value.</returns>
        public static bool IsValidGuid(string s)
        {
            return !String.IsNullOrEmpty(s) && regex.IsMatch(s);
        }
    }
}
